package com.itboye.mengwu.app.ui.aiwujia;

import java.text.SimpleDateFormat;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.luntan.comment.view.MyListView;

/**
 * 
 */

public class DetailCommentActivity extends BaseActivity implements
		OnClickListener, Observer {
	private ImageView back;
	private TextView btn_mom_collection;
	TextView txt_title;
	private TextView btn_mom_want;
	ImageView headImageView;
	private ListView momListview;
	GetAList aList;
	private TextView username_text;
	private TextView update_time;
	private TextView mom_comment_text;
	public ImageView item_image;
	View footerView;
	View headerView;
	ListView myListView;
	ViewGroup container;
	TieZiPresenter presenter;
	private ImageView img_isCollection;
	boolean isCollection;
	private String uid;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.detail_comment_layout;
	}

	@Override
	public void onMyCreate() {
		presenter = new TieZiPresenter(this);
		aList = (GetAList) getIntent().getSerializableExtra("model");
		// txt_title.setText("赠送");
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		presenter.zhutiXiangqing(uid, aList.getTid(),3+"");
		initViews();
	}

	/**
	 * initViews() 是用來初始化数据所封装的放法
	 */
	@SuppressWarnings("unchecked")
	private void initViews() {
		LayoutInflater inflater = LayoutInflater
				.from(DetailCommentActivity.this);
		headerView = inflater.inflate(R.layout.cost_detail_head_layout, null);
		footerView = inflater.inflate(R.layout.footer_mom, null);
		btn_mom_want = (TextView) footerView.findViewById(R.id.btn_mom_want);
		if (uid != aList.getAuthorId()) {
			btn_mom_want.setText("我想要");
		} else {
			btn_mom_want.setText("发货");
		}
		//
		img_isCollection = (ImageView) footerView
				.findViewById(R.id.isCollection);
		btn_mom_collection = (TextView) footerView
				.findViewById(R.id.btn_mom_collection);
		btn_mom_collection.setOnClickListener(this);
		if (Integer.parseInt(aList.getIsFav()) == 1) {
			img_isCollection.setBackgroundResource(R.drawable.collection_yi);
			isCollection = true;
		} else {
			img_isCollection.setBackgroundResource(R.drawable.collection_un);
			isCollection = false;
		}
		btn_mom_want = (TextView) footerView.findViewById(R.id.btn_mom_want);
		btn_mom_want.setOnClickListener(this);
		back = (ImageView) headerView.findViewById(R.id.back);
		back.setOnClickListener(this);
		username_text = (TextView) headerView.findViewById(R.id.username_text);
		update_time = (TextView) headerView.findViewById(R.id.update_time);
		mom_comment_text = (TextView) headerView
				.findViewById(R.id.mom_comment_text);
		headImageView = (ImageView) headerView
				.findViewById(R.id.mom_head_image);
		headImageView.setScaleType(ScaleType.CENTER_INSIDE);
		XImageLoader.load(
				GlobalConfig.API_URL + "/picture/index?id=" + aList.getHead(),
				headImageView);
		username_text.setText(aList.getAuthor());
		update_time.setText(new SimpleDateFormat("yyyy-MM-dd").format((Integer
				.parseInt(aList.getUpdateTime()) * 1000)));
		mom_comment_text.setText(aList.getMessage());
		container.addView(headerView);
		for (String s : aList.getImg()) {
			View view = LayoutInflater.from(DetailCommentActivity.this)
					.inflate(R.layout.detail_comment_item, null);
			ImageView item_image = (ImageView) view
					.findViewById(R.id.item_image);
			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id=" + s,
					item_image);
			item_image.setPadding(5, 5, 5, 5);
			container.addView(view);
		}
		for (int i = 0; i < 10; i++) {
			View view = LayoutInflater.from(DetailCommentActivity.this)
					.inflate(R.layout.item_momzeng2, null);
			Button button = (Button) view.findViewById(R.id.btn_send);
			button.setTag(i);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ByAlert.alert("i" + v.getTag());
				}
			});
			container.addView(view);
		}
		container.addView(footerView);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back: {
			finish();
			break;
		}
		case R.id.btn_mom_collection: {
			if (isCollection) {
				presenter.favorite(aList.getTid(), uid, 0 + "");
			} else {
				presenter.favorite(aList.getTid(), uid, 1 + "");
			}
			break;
		}
		case R.id.btn_mom_want: {
			Intent intent = null;
			if (btn_mom_want.getText().equals("我想要")) {
				intent = new Intent(this, WuLiuXinXiTianXieActivity.class);
			} else if (btn_mom_want.getText().equals("发货")) {
				intent = new Intent(this, ShenQingXinXiTianXieActivity.class);
			}
			startActivity(intent);
			break;
		}

		default:
			break;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == TieZiPresenter.CollectionTieziSuccess) {
				if (entity.getData().equals("取消收藏成功！")) {
					img_isCollection
							.setBackgroundResource(R.drawable.collection_un);
					isCollection = false;
				} else {
					isCollection = true;
					img_isCollection
							.setBackgroundResource(R.drawable.collection_yi);
				}
				presenter.zhutiXiangqing(uid, aList.getTid(),"3");
				ByAlert.alert(entity.getData() + "");
			} else if (entity.getEventType() == TieZiPresenter.CollectionTieziFail) {

			} else if (entity.getEventType() == TieZiPresenter.XiangQingSuccess) {
				aList = (GetAList) entity.getData();
				if (Integer.parseInt(aList.getIsFav()) == 1) {
					img_isCollection
							.setBackgroundResource(R.drawable.collection_yi);
					isCollection = true;
				} else {
					img_isCollection
							.setBackgroundResource(R.drawable.collection_un);
					isCollection = false;
				}
			}
		}
	}

	/**
	 * showInfo
	 * 
	 * 点击每个item 显示一个AlertDialog
	 * 
	 * 代表不一样的item 会有不一样的布局形式显示形式
	 * 
	 */
	// private void showInfo(int position) {
	// ImageView imv = new ImageView(DetailCommentActivity.this);
	// imv.setImageResource(R.drawable.ic_launcher);
	// new AlertDialog.Builder(DetailCommentActivity.this)
	// .setView(imv)
	// .setTitle("详情" + position)
	// .setMessage(
	// "名字：" + list.get(position) + "   价格:"
	// + list.get(position))
	// .setPositiveButton("确定",
	// new DialogInterface.OnClickListener() {
	// @Override
	// public void onClick(DialogInterface dialog,
	// int which) {
	// }
	// }).show();

	// }
}
